package work.huangxin.share.controller.business;

import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.huangxin.share.constant.BusinessConstant;
import work.huangxin.share.domain.business.BusinessInfo;
import work.huangxin.share.service.BusinessService;
import work.huangxin.share.util.status.BaseResponse;
import work.huangxin.share.util.status.CodeEnum;
import work.huangxin.share.util.status.ResponseData;

import java.util.*;


/**
 * @author 季台星
 * @Date 2023 04 22 15 09
 */
@RestController
@RequestMapping("/wx/business")
public class BusinessController {
    /**
     * 随机返回十五个商家信息
     */
    @Autowired
    private BusinessService businessService;
    private Set<Integer> randomBusinessIdSet = new HashSet<>(15);
    @GetMapping("/list")
    public BaseResponse getRandomInfo(){
        //获得商家表中总Id数据
        List<Integer> totalId = businessService.getTotalBusinessNum();
        int totalNum = totalId.size();
        if (totalNum  == 0){
            return ResponseData.error(CodeEnum.NOT_FOUND);
        }
        //配置转盘最大展示数据条数
        while(randomBusinessIdSet.size() < BusinessConstant.MAX_BUSINESS_NUM){
            Integer randomBusinessNum = new Random().nextInt(totalNum - 1) + 1;
            //set会自动去重
            randomBusinessIdSet.add(totalId.get(randomBusinessNum));
        }
        List<BusinessInfo> businessInfos = businessService.selectBusinessList(randomBusinessIdSet);
        BusinessInfo businessInfo = businessInfos.get(new Random().nextInt((businessInfos.size() - 1)) + 1);
        businessInfos.add(businessInfos.size(), businessInfo);
        if (Objects.isNull(businessInfos)){
            return ResponseData.error(CodeEnum.NOT_FOUND);
        }
        randomBusinessIdSet.clear();
        return ResponseData.success(businessInfos);
    }
    @PostMapping("/add")
    public BaseResponse addBusiness(@RequestBody BusinessInfo businessInfo){
        int i = businessService.insertBusinessInfo(businessInfo);
        if (i <= 0){
            return ResponseData.error(CodeEnum.BODY_NOT_MATCH);
        }
        return ResponseData.success();
    }
    @GetMapping("/getInfoById")
    public BaseResponse getBusinessById(@RequestParam("id") Integer businessId){
        BusinessInfo businessInfo = businessService.selectBusinessById(businessId);
        if (Objects.isNull(businessId)){
            return ResponseData.error(CodeEnum.BODY_NOT_MATCH);
        }
        return ResponseData.success(businessInfo);
    }
//    @GetMapping("/getByRandomNum")
//    public BaseResponse getRandomBusiness(){
//        if (randomBusinessIdSet.size() != 5){
//            return ResponseData.out(CodeEnum.BODY_NOT_MATCH, "请求非法!");
//        }
//        Integer[] idss = new Integer[15];
//        //获得随机数[1,totalNum)中的数字
//        Integer[] ids = randomBusinessIdSet.toArray(idss);
//        Integer id = ids[new Random().nextInt(ids.length)];
//        BusinessInfo businessInfo = businessService.getBusinessById(id);
//        if (Objects.isNull(businessInfo)){
//            return ResponseData.error(CodeEnum.BODY_NOT_MATCH);
//        }
//        //清除set集合中的元素
//        randomBusinessIdSet.clear();
//        return ResponseData.success(businessInfo);
//    }
}
